#'
#' plot the density of individuals with their spatial distribution
#'
#' @param xy a population/community object
#' @param type whether to dispaly the density of individuals by  raster or tile
#' @param contour whether to plot the contour
#'
#' @examples
#' pop=rpopulation()
#' plot_density(pop,type="raster",countour=TRUE,h=c(20,20))
#' 
#' com=rcommunity()
#' plot_density(com,type="tile",contour=TRUE,h=c(20,20))
#'

plot_density=function(xy,type="raster",contour=FALSE,h=c(1,1)){
  if(type=="raster"){
    p=plot(pop)+stat_density2d(aes(fill=..density..),geom="raster",contour=FALSE,h=h)
  }else if(type=="tile"){
    p=plot(pop)+stat_density2d(aes(alpha=..density..),geom="tile",contour=FALSE,h=h)
  }
  if(contour){
    p=p+stat_density2d(colour="grey")
  }
  return(p)
}